package com.example.mydemo103.ui.person;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydemo103.DB.UserService;
import com.example.mydemo103.LgoinActivity;
import com.example.mydemo103.R;
import com.example.mydemo103.bean.User;
import com.example.mydemo103.ui.BookAllActivity;
import com.example.mydemo103.ui.borrowbook.borrowFragment;
import com.example.mydemo103.util.Config;
import com.google.android.material.bottomappbar.BottomAppBar;

public class personFrgment extends Fragment implements View.OnClickListener {
    private TextView edit ,bookhistory,more1,readhistory,more2,aboutus,more3,personname,aboutustext;
    private User showuser;
    private Button quit;
    private LinearLayout lin1,lin2;
    String thisname="";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_person,container,false);

        edit=view.findViewById(R.id.edit);
        bookhistory=view.findViewById(R.id.bookhistory);
        more1=view.findViewById(R.id.more1);
        readhistory=view.findViewById(R.id.readhistory);
        more2=view.findViewById(R.id.more2);
        aboutus=view.findViewById(R.id.aboutus);
        more3=view.findViewById(R.id.more3);
        lin1=view.findViewById(R.id.line1);
        lin2=view.findViewById(R.id.line2);
        quit=view.findViewById(R.id.quit);
        personname=view.findViewById(R.id.PersonName);
        aboutustext=view.findViewById(R.id.aboutustext);

        quit.setOnClickListener(this);
        lin1.setOnClickListener(this);
        lin2.setOnClickListener(this);
        edit.setOnClickListener(this);
        more1.setOnClickListener(this);
        more2.setOnClickListener(this);
        more3.setOnClickListener(this);
        aboutustext.setOnClickListener(this);

        showtext(view);
        thisname=personname.getText().toString();
//        编辑个人信息
        Drawable drawablebyedit=getResources().getDrawable(R.drawable.pen);
        drawablebyedit.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        edit.setCompoundDrawables(drawablebyedit,null,null,null);
//        历史借阅记录
        Drawable drawablebybookhistory=getResources().getDrawable(R.drawable.time);
        drawablebybookhistory.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        bookhistory.setCompoundDrawables(drawablebybookhistory,null,null,null);
//        更多1
        Drawable drawablebymore1=getResources().getDrawable(R.drawable.right);
        drawablebymore1.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        more1.setCompoundDrawables(null,null,drawablebymore1,null);
//        阅读历史记录
        Drawable drawablebyreadhistory=getResources().getDrawable(R.drawable.internet);
        drawablebyreadhistory.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        readhistory.setCompoundDrawables(drawablebyreadhistory,null,null,null);
//        更多2
        Drawable drawablebymore2=getResources().getDrawable(R.drawable.right);
        drawablebymore2.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        more2.setCompoundDrawables(null,null,drawablebymore2,null);
//         关于我们
        Drawable drawablebyaboutus=getResources().getDrawable(R.drawable.question);
        drawablebyaboutus.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        aboutus.setCompoundDrawables(drawablebyaboutus,null,null,null);
//        更多3
        Drawable drawablebymore3=getResources().getDrawable(R.drawable.right);
        drawablebymore3.setBounds(0,0,45,55);//第一0是距左边距离，第二0是距上边距离，30、35分别是长宽
        more3.setCompoundDrawables(null,null,drawablebymore3,null);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.quit:
                Intent intent=new Intent(getActivity(), LgoinActivity.class);
                startActivity(intent);
                break;
            case R.id.line1:
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.nav_host_fragment, new borrowFragment(), null)
                        .addToBackStack(null)
                        .commit();
                break;
            case R.id.line2:
                Intent line2=new Intent(getActivity(), BookAllActivity.class);
                startActivity(line2);
                break;
            case R.id.edit:
                showDialog();
                break;
            case R.id.more1:
                Intent more1=new Intent(getActivity(), BookAllActivity.class);
                startActivity(more1);
                break;

            case R.id.more2:
                Intent more2=new Intent(getActivity(), BookAllActivity.class);
                startActivity(more2);
                break;
            case R.id.more3:
                aboutustext.setText("Android制作：余吉钊 173905103"+"\n"+"SSM框架给予API数据：吴黄辉 19410238"+"\n"+"文档编写：粟炜 193905133");
                break;



        }
    }
        public void showtext(View view){
        showuser= Config.auser;
        if (showuser!=null){
            personname.setText(showuser.getUsername());
//            userroot.setText( String.valueOf(showuser.getRole()));
//            sex.setText(showuser.getSex());
//            phone.setText(String.valueOf(showuser.getTelphone()));
//            time.setText(showuser.getRegtime());
//            relaname.setText(showuser.getTurname());
        }else {
            personname.setText("未登录");
        }
        };
//    }
//    public void onClick(View view){
//        switch (view.getId()){
//            case R.id.username:
//                AlertDialog.Builder build=new AlertDialog.Builder(getActivity());
//
//                break;
//        }
//    }
    private void showDialog(){
//        绑定View布局
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_username,null,false);
//        启动器？
        final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(view).create();
//        绑定buttom按钮
        Button btn_cancel_high_opion = view.findViewById(R.id.quest);
        Button btn_agree_high_opion = view.findViewById(R.id.request);
          final EditText repersonname=view.findViewById(R.id.repername);
//        确定按钮
        btn_cancel_high_opion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name=repersonname.getText().toString();
                Log.e("新用户名", name);
                personname.setText(name);
                UserService userService=new UserService(getActivity());
                userService.updateName(thisname,name);
                Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });
//        取消按钮
        btn_agree_high_opion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
//        显示对话框
        dialog.show();
//        调用这个方法时，按对话框以外的地方不起作用。按返回键也不起作用
        dialog.setCancelable(false);

//        调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
//        dialog.setCanceledOnTouchOutside(false);

//        设置窗体大小
//        dialog.getWindow().setLayout(1000,2000);
    }

}